package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 10:01  2021/10/17
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi{
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findAll(List<Long> userId) {
        Map<Long, UserInfo> map=new HashMap();
        // for (Long aLong : userId) {
        //     UserInfo userInfo = userInfoMapper.selectById(aLong);
        //     map.put(aLong,userInfo);
        // }
        QueryWrapper qw=new QueryWrapper();
        qw.in("id",userId);
        List<UserInfo> list = userInfoMapper.selectList(qw);
        for (UserInfo userInfo : list) {
            map.put(userInfo.getId(),userInfo);
        }
        return map;
    }
    //查询用户总数
    @Override
    public Integer findCount() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select("id");
        return userInfoMapper.selectCount(queryWrapper);
    }

    //查询用户
    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pageSize) {
        Page<UserInfo> page1=new Page<>(page,pageSize);
        IPage iPage=userInfoMapper.selectPage(page1,null);
        return iPage;
    }
    //自建
    public IPage<UserInfo> selectAll(Integer page, Integer pagesize) {

        // 指定分页参数
        Page<UserInfo> pageParam  = new Page<>(page,pagesize);

        // 查询
        Page<UserInfo> iPage = userInfoMapper.selectPage(pageParam, null);

        return iPage;
    }

    @Override
    public Map<Long, UserInfo> findIds(List<Long> ids) {
        Map<Long, UserInfo> map = new HashMap<>();
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);

        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);

        for (UserInfo userInfo : userInfoList) {
            map.put(userInfo.getId(), userInfo);
        }
        return map;
    }
}
